import Vue from 'vue'
import Vuex from 'vuex'
import _ from 'lodash'
import Toast from 'vant'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {   
    shoplist:[],
    goodslist:{}
  },
  getters:{
    shoplist:state=>{
      return state.shoplist
    },
    goodslist:state=>{
      return state.goodslist
    }
  },
  mutations: {
    addcart:(state,payload)=>{
      var a=_.findIndex(state.shoplist,{id:payload.id});
      if(a==-1){
        state.shoplist.push({...payload,number:1})
      }else{
        state.shoplist[a].number++
      }  
  },
  increase:(state,payload)=>{
      var a=_.findIndex(state.shoplist,{id:payload.id});
      state.shoplist[a].number+=1
    },
    reduce:(state,payload)=>{
      var a=_.findIndex(state.shoplist,{id:payload.id});
      if(state.shoplist[a].number>1){
        state.shoplist[a].number-=1
      }else{
        state.shoplist[a].number=1
      }
      
    },
    goods:(state,payload)=>{
      state.goodslist=payload
     
      
    }
  },
  actions: {
    addcart:({commit},payload)=>{
      commit('addcart',payload)
    },
    increase:({commit},payload)=>{
      commit('increase',payload)
    },
    reduce:({commit},payload)=>{
      commit('reduce',payload)
    },
    goods:({commit},payload)=>{
      commit('goods',payload)
    }
 }
})
